using Digitalmes.Infrastructure.IO;

namespace Digitalmes.WebApi.Endpoints;

/// <summary>
/// Mock 终结点，供开发测试使用。
/// </summary>
public sealed class MockEndpoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this)
            .MapGet(ExportExcelFileAsync, "excel/export/file")
            .MapGet(ExportExcelBlobAsync, "excel/export/blob");
    }

    [EndpointDescription("测试 Excel 文件导出")]
    public IResult ExportExcelFileAsync(IExcelExport excel)
    {
        List<MockModel> data = [new() {
            Name = "Apple",
            Age = 22,
            CreatedTime = DateTime.Now,
        }, new() {
            Name = "Bnana",
            Age = 23,
            CreatedTime = DateTime.Now,
        }];
        var ms = excel.SaveAsStream(data);

        // .xlsx（2010+ Excel）
        return Results.File(ms, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", $"Mock_{DateTime.Now:yyyyMMddHHmmss}.xlsx");
    }

    [EndpointDescription("测试 Excel blob 文件导出")]
    public IResult ExportExcelBlobAsync(IExcelExport excel)
    {
        List<MockModel> data = [new() {
            Name = "Apple",
            Age = 22,
            CreatedTime = DateTime.Now,
        }, new() {
            Name = "Bnana",
            Age = 23,
            CreatedTime = DateTime.Now,
        }];
        var ms = excel.SaveAsStream(data);
        return Results.Stream(ms, "application/octet-stream");
    }

    public sealed class MockModel
    {
        [DisplayName("姓名")]
        public string? Name { get; set; }

        [DisplayName("年龄")]
        public int Age { get; set; }

        [DisplayName("创建时间")]
        public DateTime CreatedTime { get; set; }
    }
}
